class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> hash;
        for (auto& s : wordDict)
            hash.insert(s);
        int n = s.size();
        vector<bool> dp(n + 1);//dp[i]  表⽰： [0, i]  区间内的字符串，能否被字典中的单词拼接⽽成
        dp[0] = true;                
        s = ' ' + s;                
        for (int i = 1; i <= n; i++) 
        {
            for (int j = i; j >= 1; j--) 
            {
                if (dp[j - 1] && hash.count(s.substr(j, i - j + 1))) {
                    dp[i] = true;
                    break; 
                }
            }
        }
        return dp[n];
    }
};